home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / random.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  23.1 KB  |  765 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Random variable generators.
  5.  
  6.     integers
  7.     --------
  8.            uniform within range
  9.  
  10.     sequences
  11.     ---------
  12.            pick random element
  13.            pick random sample
  14.            generate random permutation
  15.  
  16.     distributions on the real line:
  17.     ------------------------------
  18.            uniform
  19.            normal (Gaussian)
  20.            lognormal
  21.            negative exponential
  22.            gamma
  23.            beta
  24.            pareto
  25.            Weibull
  26.  
  27.     distributions on the circle (angles 0 to 2pi)
  28.     ---------------------------------------------
  29.            circular uniform
  30.            von Mises
  31.  
  32. General notes on the underlying Mersenne Twister core generator:
  33.  
  34. * The period is 2**19937-1.
  35. * It is one of the most extensively tested generators in existence.
  36. * Without a direct way to compute N steps forward, the semantics of
  37.   jumpahead(n) are weakened to simply jump to another distant state and rely
  38.   on the large period to avoid overlapping sequences.
  39. * The random() method is implemented in C, executes in a single Python step,
  40.   and is, therefore, threadsafe.
  41.  
  42. '''
  43. from warnings import warn as _warn
  44. from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
  45. from math import log as _log, exp as _exp, pi as _pi, e as _e
  46. from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
  47. from os import urandom as _urandom
  48. from binascii import hexlify as _hexlify
  49. __all__ = [
  50.     'Random',
  51.     'seed',
  52.     'random',
  53.     'uniform',
  54.     'randint',
  55.     'choice',
  56.     'sample',
  57.     'randrange',
  58.     'shuffle',
  59.     'normalvariate',
  60.     'lognormvariate',
  61.     'expovariate',
  62.     'vonmisesvariate',
  63.     'gammavariate',
  64.     'gauss',
  65.     'betavariate',
  66.     'paretovariate',
  67.     'weibullvariate',
  68.     'getstate',
  69.     'setstate',
  70.     'jumpahead',
  71.     'WichmannHill',
  72.     'getrandbits',
  73.     'SystemRandom']
  74. NV_MAGICCONST = 4 * _exp(-0.5) / _sqrt(2.0)
  75. TWOPI = 2.0 * _pi
  76. LOG4 = _log(4.0)
  77. SG_MAGICCONST = 1.0 + _log(4.5)
  78. BPF = 53
  79. RECIP_BPF = 2 ** (-BPF)
  80. import _random
  81.  
  82. class Random(_random.Random):
  83.     """Random number generator base class used by bound module functions.
  84.  
  85.     Used to instantiate instances of Random to get generators that don't
  86.     share state.  Especially useful for multi-threaded programs, creating
  87.     a different instance of Random for each thread, and using the jumpahead()
  88.     method to ensure that the generated sequences seen by each thread don't
  89.     overlap.
  90.  
  91.     Class Random can also be subclassed if you want to use a different basic
  92.     generator of your own devising: in that case, override the following
  93.     methods:  random(), seed(), getstate(), setstate() and jumpahead().
  94.     Optionally, implement a getrandombits() method so that randrange()
  95.     can cover arbitrarily large ranges.
  96.  
  97.     """
  98.     VERSION = 2
  99.     
  100.     def __init__(self, x = None):
  101.         '''Initialize an instance.
  102.  
  103.         Optional argument x controls seeding, as for Random.seed().
  104.         '''
  105.         self.seed(x)
  106.         self.gauss_next = None
  107.  
  108.     
  109.     def seed(self, a = None):
  110.         '''Initialize internal state from hashable object.
  111.  
  112.         None or no argument seeds from current time or from an operating
  113.         system specific randomness source if available.
  114.  
  115.         If a is not None or an int or long, hash(a) is used instead.
  116.         '''
  117.         if a is None:
  118.             
  119.             try:
  120.                 a = long(_hexlify(_urandom(16)), 16)
  121.             except NotImplementedError:
  122.                 import time
  123.                 a = long(time.time() * 256)
  124.             except:
  125.                 None<EXCEPTION MATCH>NotImplementedError
  126.             
  127.  
  128.         None<EXCEPTION MATCH>NotImplementedError
  129.         super(Random, self).seed(a)
  130.         self.gauss_next = None
  131.  
  132.     
  133.     def getstate(self):
  134.         '''Return internal state; can be passed to setstate() later.'''
  135.         return (self.VERSION, super(Random, self).getstate(), self.gauss_next)
  136.  
  137.     
  138.     def setstate(self, state):
  139.         '''Restore internal state from object returned by getstate().'''
  140.         version = state[0]
  141.         if version == 2:
  142.             (version, internalstate, self.gauss_next) = state
  143.             super(Random, self).setstate(internalstate)
  144.         else:
  145.             raise ValueError('state with version %s passed to Random.setstate() of version %s' % (version, self.VERSION))
  146.  
  147.     
  148.     def __getstate__(self):
  149.         return self.getstate()
  150.  
  151.     
  152.     def __setstate__(self, state):
  153.         self.setstate(state)
  154.  
  155.     
  156.     def __reduce__(self):
  157.         return (self.__class__, (), self.getstate())
  158.  
  159.     
  160.     def randrange(self, start, stop = None, step = 1, int = int, default = None, maxwidth = 0x1L << BPF):
  161.         """Choose a random item from range(start, stop[, step]).
  162.  
  163.         This fixes the problem with randint() which includes the
  164.         endpoint; in Python this is usually not what you want.
  165.         Do not supply the 'int', 'default', and 'maxwidth' arguments.
  166.         """
  167.         istart = int(start)
  168.         if istart != start:
  169.             raise ValueError, 'non-integer arg 1 for randrange()'
  170.         
  171.         if stop is default:
  172.             if istart > 0:
  173.                 if istart >= maxwidth:
  174.                     return self._randbelow(istart)
  175.                 
  176.                 return int(self.random() * istart)
  177.             
  178.             raise ValueError, 'empty range for randrange()'
  179.         
  180.         istop = int(stop)
  181.         if istop != stop:
  182.             raise ValueError, 'non-integer stop for randrange()'
  183.         
  184.         width = istop - istart
  185.         if step == 1 and width > 0:
  186.             if width >= maxwidth:
  187.                 return int(istart + self._randbelow(width))
  188.             
  189.             return int(istart + int(self.random() * width))
  190.         
  191.         if step == 1:
  192.             raise ValueError, 'empty range for randrange() (%d,%d, %d)' % (istart, istop, width)
  193.         
  194.         istep = int(step)
  195.         if istep != step:
  196.             raise ValueError, 'non-integer step for randrange()'
  197.         
  198.         if istep > 0:
  199.             n = (width + istep - 1) // istep
  200.         elif istep < 0:
  201.             n = (width + istep + 1) // istep
  202.         else:
  203.             raise ValueError, 'zero step for randrange()'
  204.         if n <= 0:
  205.             raise ValueError, 'empty range for randrange()'
  206.         
  207.         if n >= maxwidth:
  208.             return istart + self._randbelow(n)
  209.         
  210.         return istart + istep * int(self.random() * n)
  211.  
  212.     
  213.     def randint(self, a, b):
  214.         '''Return random integer in range [a, b], including both end points.
  215.         '''
  216.         return self.randrange(a, b + 1)
  217.  
  218.     
  219.     def _randbelow(self, n, _log = _log, int = int, _maxwidth = 0x1L << BPF, _Method = _MethodType, _BuiltinMethod = _BuiltinMethodType):
  220.         '''Return a random int in the range [0,n)
  221.  
  222.         Handles the case where n has more bits than returned
  223.         by a single call to the underlying generator.
  224.         '''
  225.         
  226.         try:
  227.             getrandbits = self.getrandbits
  228.         except AttributeError:
  229.             pass
  230.  
  231.         if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
  232.             k = int(1.0000100000000001 + _log(n - 1, 2.0))
  233.             r = getrandbits(k)
  234.             while r >= n:
  235.                 r = getrandbits(k)
  236.             return r
  237.         
  238.         if n >= _maxwidth:
  239.             _warn('Underlying random() generator does not supply \nenough bits to choose from a population range this large')
  240.         
  241.         return int(self.random() * n)
  242.  
  243.     
  244.     def choice(self, seq):
  245.         '''Choose a random element from a non-empty sequence.'''
  246.         return seq[int(self.random() * len(seq))]
  247.  
  248.     
  249.     def shuffle(self, x, random = None, int = int):
  250.         '''x, random=random.random -> shuffle list x in place; return None.
  251.  
  252.         Optional arg random is a 0-argument function returning a random
  253.         float in [0.0, 1.0); by default, the standard random.random.
  254.         '''
  255.         if random is None:
  256.             random = self.random
  257.         
  258.         for i in reversed(xrange(1, len(x))):
  259.             j = int(random() * (i + 1))
  260.             x[i] = x[j]
  261.             x[j] = x[i]
  262.         
  263.  
  264.     
  265.     def sample(self, population, k):
  266.         '''Chooses k unique random elements from a population sequence.
  267.  
  268.         Returns a new list containing elements from the population while
  269.         leaving the original population unchanged.  The resulting list is
  270.         in selection order so that all sub-slices will also be valid random
  271.         samples.  This allows raffle winners (the sample) to be partitioned
  272.         into grand prize and second place winners (the subslices).
  273.  
  274.         Members of the population need not be hashable or unique.  If the
  275.         population contains repeats, then each occurrence is a possible
  276.         selection in the sample.
  277.  
  278.         To choose a sample in a range of integers, use xrange as an argument.
  279.         This is especially fast and space efficient for sampling from a
  280.         large population:   sample(xrange(10000000), 60)
  281.         '''
  282.         n = len(population)
  283.         if k <= k:
  284.             pass
  285.         elif not k <= n:
  286.             raise ValueError, 'sample larger than population'
  287.         
  288.         random = self.random
  289.         _int = int
  290.         result = [
  291.             None] * k
  292.         if n < 6 * k or hasattr(population, 'keys'):
  293.             pool = list(population)
  294.             for i in xrange(k):
  295.                 j = _int(random() * (n - i))
  296.                 result[i] = pool[j]
  297.                 pool[j] = pool[n - i - 1]
  298.             
  299.         else:
  300.             
  301.             try:
  302.                 selected = { }
  303.                 for i in xrange(k):
  304.                     j = _int(random() * n)
  305.                     while j in selected:
  306.                         j = _int(random() * n)
  307.                     result[i] = selected[j] = population[j]
  308.             except (TypeError, KeyError):
  309.                 if isinstance(population, list):
  310.                     raise 
  311.                 
  312.                 return self.sample(tuple(population), k)
  313.  
  314.         return result
  315.  
  316.     
  317.     def uniform(self, a, b):
  318.         '''Get a random number in the range [a, b).'''
  319.         return a + (b - a) * self.random()
  320.  
  321.     
  322.     def normalvariate(self, mu, sigma):
  323.         '''Normal distribution.
  324.  
  325.         mu is the mean, and sigma is the standard deviation.
  326.  
  327.         '''
  328.         random = self.random
  329.         while None:
  330.             u1 = random()
  331.             u2 = 1.0 - random()
  332.             z = NV_MAGICCONST * (u1 - 0.5) / u2
  333.             zz = z * z / 4.0
  334.             if zz <= -_log(u2):
  335.                 break
  336.                 continue
  337.         return mu + z * sigma
  338.  
  339.     
  340.     def lognormvariate(self, mu, sigma):
  341.         """Log normal distribution.
  342.  
  343.         If you take the natural logarithm of this distribution, you'll get a
  344.         normal distribution with mean mu and standard deviation sigma.
  345.         mu can have any value, and sigma must be greater than zero.
  346.  
  347.         """
  348.         return _exp(self.normalvariate(mu, sigma))
  349.  
  350.     
  351.     def expovariate(self, lambd):
  352.         '''Exponential distribution.
  353.  
  354.         lambd is 1.0 divided by the desired mean.  (The parameter would be
  355.         called "lambda", but that is a reserved word in Python.)  Returned
  356.         values range from 0 to positive infinity.
  357.  
  358.         '''
  359.         random = self.random
  360.         u = random()
  361.         while u <= 9.9999999999999995e-08:
  362.             u = random()
  363.         return -_log(u) / lambd
  364.  
  365.     
  366.     def vonmisesvariate(self, mu, kappa):
  367.         '''Circular data distribution.
  368.  
  369.         mu is the mean angle, expressed in radians between 0 and 2*pi, and
  370.         kappa is the concentration parameter, which must be greater than or
  371.         equal to zero.  If kappa is equal to zero, this distribution reduces
  372.         to a uniform random angle over the range 0 to 2*pi.
  373.  
  374.         '''
  375.         random = self.random
  376.         if kappa <= 9.9999999999999995e-07:
  377.             return TWOPI * random()
  378.         
  379.         a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
  380.         b = (a - _sqrt(2.0 * a)) / (2.0 * kappa)
  381.         r = (1.0 + b * b) / (2.0 * b)
  382.         while None:
  383.             u1 = random()
  384.             z = _cos(_pi * u1)
  385.             f = (1.0 + r * z) / (r + z)
  386.             c = kappa * (r - f)
  387.             u2 = random()
  388.             if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c):
  389.                 break
  390.                 continue
  391.         u3 = random()
  392.         if u3 > 0.5:
  393.             theta = mu % TWOPI + _acos(f)
  394.         else:
  395.             theta = mu % TWOPI - _acos(f)
  396.         return theta
  397.  
  398.     
  399.     def gammavariate(self, alpha, beta):
  400.         '''Gamma distribution.  Not the gamma function!
  401.  
  402.         Conditions on the parameters are alpha > 0 and beta > 0.
  403.  
  404.         '''
  405.         if alpha <= 0.0 or beta <= 0.0:
  406.             raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
  407.         
  408.         random = self.random
  409.         if alpha > 1.0:
  410.             ainv = _sqrt(2.0 * alpha - 1.0)
  411.             bbb = alpha - LOG4
  412.             ccc = alpha + ainv
  413.             while None:
  414.                 u1 = random()
  415.                 if u1 < u1:
  416.                     pass
  417.                 elif not u1 < 0.99999990000000005:
  418.                     continue
  419.                 
  420.                 u2 = 1.0 - random()
  421.                 v = _log(u1 / (1.0 - u1)) / ainv
  422.                 x = alpha * _exp(v)
  423.                 z = u1 * u1 * u2
  424.                 r = bbb + ccc * v - x
  425.                 if r + SG_MAGICCONST - 4.5 * z >= 0.0 or r >= _log(z):
  426.                     return x * beta
  427.                     continue
  428.         elif alpha == 1.0:
  429.             u = random()
  430.             while u <= 9.9999999999999995e-08:
  431.                 u = random()
  432.             return -_log(u) * beta
  433.         else:
  434.             while None:
  435.                 u = random()
  436.                 b = (_e + alpha) / _e
  437.                 p = b * u
  438.                 if p <= 1.0:
  439.                     x = p ** (1.0 / alpha)
  440.                 else:
  441.                     x = -_log((b - p) / alpha)
  442.                 u1 = random()
  443.                 if p > 1.0:
  444.                     if u1 <= x ** (alpha - 1.0):
  445.                         break
  446.                     
  447.                 if u1 <= _exp(-x):
  448.                     break
  449.                     continue
  450.             return x * beta
  451.  
  452.     
  453.     def gauss(self, mu, sigma):
  454.         '''Gaussian distribution.
  455.  
  456.         mu is the mean, and sigma is the standard deviation.  This is
  457.         slightly faster than the normalvariate() function.
  458.  
  459.         Not thread-safe without a lock around calls.
  460.  
  461.         '''
  462.         random = self.random
  463.         z = self.gauss_next
  464.         self.gauss_next = None
  465.         if z is None:
  466.             x2pi = random() * TWOPI
  467.             g2rad = _sqrt(-2.0 * _log(1.0 - random()))
  468.             z = _cos(x2pi) * g2rad
  469.             self.gauss_next = _sin(x2pi) * g2rad
  470.         
  471.         return mu + z * sigma
  472.  
  473.     
  474.     def betavariate(self, alpha, beta):
  475.         '''Beta distribution.
  476.  
  477.         Conditions on the parameters are alpha > -1 and beta} > -1.
  478.         Returned values range between 0 and 1.
  479.  
  480.         '''
  481.         y = self.gammavariate(alpha, 1.0)
  482.         if y == 0:
  483.             return 0.0
  484.         else:
  485.             return y / (y + self.gammavariate(beta, 1.0))
  486.  
  487.     
  488.     def paretovariate(self, alpha):
  489.         '''Pareto distribution.  alpha is the shape parameter.'''
  490.         u = 1.0 - self.random()
  491.         return 1.0 / pow(u, 1.0 / alpha)
  492.  
  493.     
  494.     def weibullvariate(self, alpha, beta):
  495.         '''Weibull distribution.
  496.  
  497.         alpha is the scale parameter and beta is the shape parameter.
  498.  
  499.         '''
  500.         u = 1.0 - self.random()
  501.         return alpha * pow(-_log(u), 1.0 / beta)
  502.  
  503.  
  504.  
  505. class WichmannHill(Random):
  506.     VERSION = 1
  507.     
  508.     def seed(self, a = None):
  509.         '''Initialize internal state from hashable object.
  510.  
  511.         None or no argument seeds from current time or from an operating
  512.         system specific randomness source if available.
  513.  
  514.         If a is not None or an int or long, hash(a) is used instead.
  515.  
  516.         If a is an int or long, a is used directly.  Distinct values between
  517.         0 and 27814431486575L inclusive are guaranteed to yield distinct
  518.         internal states (this guarantee is specific to the default
  519.         Wichmann-Hill generator).
  520.         '''
  521.         if a is None:
  522.             
  523.             try:
  524.                 a = long(_hexlify(_urandom(16)), 16)
  525.             except NotImplementedError:
  526.                 import time
  527.                 a = long(time.time() * 256)
  528.             except:
  529.                 None<EXCEPTION MATCH>NotImplementedError
  530.             
  531.  
  532.         None<EXCEPTION MATCH>NotImplementedError
  533.         if not isinstance(a, (int, long)):
  534.             a = hash(a)
  535.         
  536.         (a, x) = divmod(a, 30268)
  537.         (a, y) = divmod(a, 30306)
  538.         (a, z) = divmod(a, 30322)
  539.         self._seed = (int(x) + 1, int(y) + 1, int(z) + 1)
  540.         self.gauss_next = None
  541.  
  542.     
  543.     def random(self):
  544.         '''Get the next random number in the range [0.0, 1.0).'''
  545.         (x, y, z) = self._seed
  546.         x = 171 * x % 30269
  547.         y = 172 * y % 30307
  548.         z = 170 * z % 30323
  549.         self._seed = (x, y, z)
  550.         return (x / 30269.0 + y / 30307.0 + z / 30323.0) % 1.0
  551.  
  552.     
  553.     def getstate(self):
  554.         '''Return internal state; can be passed to setstate() later.'''
  555.         return (self.VERSION, self._seed, self.gauss_next)
  556.  
  557.     
  558.     def setstate(self, state):
  559.         '''Restore internal state from object returned by getstate().'''
  560.         version = state[0]
  561.         if version == 1:
  562.             (version, self._seed, self.gauss_next) = state
  563.         else:
  564.             raise ValueError('state with version %s passed to Random.setstate() of version %s' % (version, self.VERSION))
  565.  
  566.     
  567.     def jumpahead(self, n):
  568.         '''Act as if n calls to random() were made, but quickly.
  569.  
  570.         n is an int, greater than or equal to 0.
  571.  
  572.         Example use:  If you have 2 threads and know that each will
  573.         consume no more than a million random numbers, create two Random
  574.         objects r1 and r2, then do
  575.             r2.setstate(r1.getstate())
  576.             r2.jumpahead(1000000)
  577.         Then r1 and r2 will use guaranteed-disjoint segments of the full
  578.         period.
  579.         '''
  580.         if not n >= 0:
  581.             raise ValueError('n must be >= 0')
  582.         
  583.         (x, y, z) = self._seed
  584.         x = int(x * pow(171, n, 30269)) % 30269
  585.         y = int(y * pow(172, n, 30307)) % 30307
  586.         z = int(z * pow(170, n, 30323)) % 30323
  587.         self._seed = (x, y, z)
  588.  
  589.     
  590.     def __whseed(self, x = 0, y = 0, z = 0):
  591.         '''Set the Wichmann-Hill seed from (x, y, z).
  592.  
  593.         These must be integers in the range [0, 256).
  594.         '''
  595.         if type(y) == type(y) and type(z) == type(z):
  596.             pass
  597.         elif not type(z) == int:
  598.             raise TypeError('seeds must be integers')
  599.         
  600.         if x <= x:
  601.             pass
  602.         elif x < 256:
  603.             if y <= y:
  604.                 pass
  605.             elif y < 256:
  606.                 if z <= z:
  607.                     pass
  608.                 elif not z < 256:
  609.                     raise ValueError('seeds must be in range(0, 256)')
  610.                 
  611.         if x == x and y == y:
  612.             pass
  613.         elif y == z:
  614.             import time
  615.             t = long(time.time() * 256)
  616.             t = int(t & 16777215 ^ t >> 24)
  617.             (t, x) = divmod(t, 256)
  618.             (t, y) = divmod(t, 256)
  619.             (t, z) = divmod(t, 256)
  620.         
  621.         if not x:
  622.             pass
  623.         if not y:
  624.             pass
  625.         if not z:
  626.             pass
  627.         self._seed = (1, 1, 1)
  628.         self.gauss_next = None
  629.  
  630.     
  631.     def whseed(self, a = None):
  632.         """Seed from hashable object's hash code.
  633.  
  634.         None or no argument seeds from current time.  It is not guaranteed
  635.         that objects with distinct hash codes lead to distinct internal
  636.         states.
  637.  
  638.         This is obsolete, provided for compatibility with the seed routine
  639.         used prior to Python 2.1.  Use the .seed() method instead.
  640.         """
  641.         if a is None:
  642.             self._WichmannHill__whseed()
  643.             return None
  644.         
  645.         a = hash(a)
  646.         (a, x) = divmod(a, 256)
  647.         (a, y) = divmod(a, 256)
  648.         (a, z) = divmod(a, 256)
  649.         if not (x + a) % 256:
  650.             pass
  651.         x = 1
  652.         if not (y + a) % 256:
  653.             pass
  654.         y = 1
  655.         if not (z + a) % 256:
  656.             pass
  657.         z = 1
  658.         self._WichmannHill__whseed(x, y, z)
  659.  
  660.  
  661.  
  662. class SystemRandom(Random):
  663.     '''Alternate random number generator using sources provided
  664.     by the operating system (such as /dev/urandom on Unix or
  665.     CryptGenRandom on Windows).
  666.  
  667.      Not available on all systems (see os.urandom() for details).
  668.     '''
  669.     
  670.     def random(self):
  671.         '''Get the next random number in the range [0.0, 1.0).'''
  672.         return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF
  673.  
  674.     
  675.     def getrandbits(self, k):
  676.         '''getrandbits(k) -> x.  Generates a long int with k random bits.'''
  677.         if k <= 0:
  678.             raise ValueError('number of bits must be greater than zero')
  679.         
  680.         if k != int(k):
  681.             raise TypeError('number of bits should be an integer')
  682.         
  683.         bytes = (k + 7) // 8
  684.         x = long(_hexlify(_urandom(bytes)), 16)
  685.         return x >> bytes * 8 - k
  686.  
  687.     
  688.     def _stub(self, *args, **kwds):
  689.         '''Stub method.  Not used for a system random number generator.'''
  690.         pass
  691.  
  692.     seed = jumpahead = _stub
  693.     
  694.     def _notimplemented(self, *args, **kwds):
  695.         '''Method should not be called for a system random number generator.'''
  696.         raise NotImplementedError('System entropy source does not have state.')
  697.  
  698.     getstate = setstate = _notimplemented
  699.  
  700.  
  701. def _test_generator(n, func, args):
  702.     import time
  703.     print n, 'times', func.__name__
  704.     total = 0.0
  705.     sqsum = 0.0
  706.     smallest = 10000000000.0
  707.     largest = -10000000000.0
  708.     t0 = time.time()
  709.     for i in range(n):
  710.         x = func(*args)
  711.         total += x
  712.         sqsum = sqsum + x * x
  713.         smallest = min(x, smallest)
  714.         largest = max(x, largest)
  715.     
  716.     t1 = time.time()
  717.     print round(t1 - t0, 3), 'sec,',
  718.     avg = total / n
  719.     stddev = _sqrt(sqsum / n - avg * avg)
  720.     print 'avg %g, stddev %g, min %g, max %g' % (avg, stddev, smallest, largest)
  721.  
  722.  
  723. def _test(N = 2000):
  724.     _test_generator(N, random, ())
  725.     _test_generator(N, normalvariate, (0.0, 1.0))
  726.     _test_generator(N, lognormvariate, (0.0, 1.0))
  727.     _test_generator(N, vonmisesvariate, (0.0, 1.0))
  728.     _test_generator(N, gammavariate, (0.01, 1.0))
  729.     _test_generator(N, gammavariate, (0.10000000000000001, 1.0))
  730.     _test_generator(N, gammavariate, (0.10000000000000001, 2.0))
  731.     _test_generator(N, gammavariate, (0.5, 1.0))
  732.     _test_generator(N, gammavariate, (0.90000000000000002, 1.0))
  733.     _test_generator(N, gammavariate, (1.0, 1.0))
  734.     _test_generator(N, gammavariate, (2.0, 1.0))
  735.     _test_generator(N, gammavariate, (20.0, 1.0))
  736.     _test_generator(N, gammavariate, (200.0, 1.0))
  737.     _test_generator(N, gauss, (0.0, 1.0))
  738.     _test_generator(N, betavariate, (3.0, 3.0))
  739.  
  740. _inst = Random()
  741. seed = _inst.seed
  742. random = _inst.random
  743. uniform = _inst.uniform
  744. randint = _inst.randint
  745. choice = _inst.choice
  746. randrange = _inst.randrange
  747. sample = _inst.sample
  748. shuffle = _inst.shuffle
  749. normalvariate = _inst.normalvariate
  750. lognormvariate = _inst.lognormvariate
  751. expovariate = _inst.expovariate
  752. vonmisesvariate = _inst.vonmisesvariate
  753. gammavariate = _inst.gammavariate
  754. gauss = _inst.gauss
  755. betavariate = _inst.betavariate
  756. paretovariate = _inst.paretovariate
  757. weibullvariate = _inst.weibullvariate
  758. getstate = _inst.getstate
  759. setstate = _inst.setstate
  760. jumpahead = _inst.jumpahead
  761. getrandbits = _inst.getrandbits
  762. if __name__ == '__main__':
  763.     _test()
  764.  
  765.